//
//  HELCreditVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/21.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELCreditVC.h"
#import "HELCreditTableVC.h"
#import "HELCreditHeader.h"
#import "SSPopup.h"
#import "HELCreditApplyVC.h"
#import "HELHistoryCheckVC.h"
@interface HELCreditVC ()

@property (nonatomic,  strong) NSArray *creditTypes;
@property (nonatomic,  strong) HELCreditHeader *header;

@property (assign, nonatomic) NSInteger  index;


@end

@implementation HELCreditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addLine];
    self.index = 0;
    self.header = [HELCreditHeader sharedHeader];
  
    [self.view addSubview:self.header];
    weakSelf_define;
    self.header.backAction = ^(){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    self.header.apply = ^(){
        
        [weakSelf queryCreditType];
    
    };
   
    self.header.historyCheck = ^(){
        [weakSelf.navigationController setNavigationBarHidden:NO animated:YES];
        [weakSelf.navigationController pushViewController:[HELHistoryCheckVC new] animated:YES];
    
    };
    
}
- (void)viewWillAppear:(BOOL)animated{
   
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)queryCreditType{
 
    
        SSPopup* selection=[[SSPopup alloc]init];
        selection.backgroundColor=[UIColor colorWithWhite:0.00 alpha:0.4];
        
        selection.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
        
        [self.view  addSubview:selection];
        weakSelf_define;
        [selection CreateTableview:@[@"一类",@"二类"] withSender:nil  withTitle:@"考试选择" setCompletionBlock:^(int tag){
            HELCreditApplyVC *vc =[HELCreditApplyVC new];
            vc.creditName =@[@"一类",@"二类"][tag];
            [weakSelf.navigationController setNavigationBarHidden:NO];
            [weakSelf.navigationController pushViewController:vc animated:YES];

            
        }];

  
  

}








- (NSArray *)creditTypes{
    if (!_creditTypes) {
        _creditTypes = @[@"I  类学分",@"II  类学分"];
    }
    return _creditTypes;
}

- (instancetype)init {
    if (self = [super init]) {
        self.titleSizeNormal = 17;
        self.titleSizeSelected = 17;
        self.menuViewStyle = WMMenuViewStyleLine;
        self.menuItemWidth = [UIScreen mainScreen].bounds.size.width / self.creditTypes.count;
        self.menuHeight = 49;
        self.menuView.speedFactor = 3;
        
        self.menuBGColor = [UIColor whiteColor];
        self.titleColorSelected =kMainColor;
        self.titleColorNormal =[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
        
    }
    return self;
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.viewFrame = CGRectMake(0, 200, kScreenWidth, kScreenHeight);
    self.header.frame = CGRectMake(0, -20, kScreenWidth, 220);
}



- (void)addLine{
    UIView *line = [UIView new];
    line.backgroundColor = kBorderColor;
    line.frame = CGRectMake(0, 48, kScreenWidth, 1);
    [self.menuView insertSubview:line atIndex:0];
    
}

- (void)getMyCredit{
  
    weakSelf_define;
    
    
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    if (self.index == 0) {
        params[@"scoreType"] = @"一类";
    }else{
        params[@"scoreType"] = @"二类";
    }
    
    
    [[HELNetworkManager sharedMg] POST:kGetMycredit parameters:params success:^(id responseObject) {
        if (![responseObject[@"data"] isKindOfClass:[NSNull class]]) {
//            weakSelf.header.scoreL.text = [NSString stringWithFormat:@"%@",responseObject[@"data"]];
            [weakSelf.header setCredit:responseObject[@"data"]];
           
        }else{
             [weakSelf.header setCredit:@(0)];
        }
        
    } failure:^(NSString *err) {
        
    }];
 
}



#pragma mark - Datasource & Delegate
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.creditTypes.count;
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
   HELCreditTableVC *vc = [HELCreditTableVC new];
    if (index == 0) {
        vc.typeName = @"一类";
        
    }else{
         vc.typeName = @"二类";
    }
    
    
    return vc;
    
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.creditTypes[index];
}
- (void)pageController:(WMPageController *)pageController willEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    
    HELCreditTableVC *vc = viewController;
   
    [vc requestDataFirst];
  
    
}

- (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info
{
     self.index =  [info[@"index"] longValue];
      [self getMyCredit];

}

@end
